package util;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;

import java.util.List;

import base.MvpApplication;


public class CookieUtil {

    private Context mContext;

    public CookieUtil(Context mContext) {
        this.mContext = mContext;
    }

    public String getString(String key) {
        return SharedPreferencesUtil.get(mContext, key, "");
    }

    public <T> T getObject(String key, Class<T> clazz) {
        String cache = SharedPreferencesUtil.get(mContext, key, "");
        if (TextUtils.isEmpty(cache)) {
            return null;
        }
        return JSON.parseObject(cache, clazz);
    }

    public <T> List<T> getArray(String key, Class<T> clazz) {
        String cache = SharedPreferencesUtil.get(mContext == null ? MvpApplication.getInstance() : mContext, key, "");
        if (TextUtils.isEmpty(cache)) {
            return null;
        }
        return JSON.parseArray(cache, clazz);
//        return  new Gson().fromJson(cache,new TypeToken<List<T>>(){}.getType());
    }

    public void putObject(String key, Object object) {
        String  value=JSON.toJSONString(object);
        SharedPreferencesUtil.put(mContext, key,value );
    }

    public  void putString(String key,String value){
        SharedPreferencesUtil.put(mContext,key,value);
    }

    public void removeObject(String key) {
        SharedPreferencesUtil.clear(mContext, key);
    }


}
